<template>
  <rx-dialog style="height: 100%;" @handOk="okBtn" @cancel="cancel">
    <a-form :form="form">
        <div class="myHeader" v-if="show">
            <a-radio style="width: 120px" @change="onChangeAll" v-model="allType">{{i18n('list.everyone','所有人')}}</a-radio>
            <a-radio style="width: 120px" @change="onChangeNone" v-model="noneType">{{i18n('list.none','无权限')}}</a-radio>
            <a-radio style="width: 120px" @change="onChangeShowList" v-model="showList">{{i18n('list.custom','自定义')}}</a-radio>
        </div>
      <template v-if="showList" >
        <ul class="container-box">
            <li v-for="(dataObj, index) in typeList">
              <form-user-or-group ref="preview" :dataObj="dataObj"></form-user-or-group>
            </li>
        </ul>
      </template>
        <div class="myHeader" v-if="attachmentList">
            <a-checkbox style="width: 120px"  v-if="type=='edit' " v-model="deleteAttach">{{i18n('list.delAttach','删除附件')}}</a-checkbox>
            <a-checkbox style="width: 120px" v-if="type!='require'"  v-model="downloadAttach">{{i18n('list.downAttach','下载附件')}}</a-checkbox>
        </div>
    </a-form>
  </rx-dialog>
</template>
<script>
  import FormPermissionApi from '@/api/form/core/formPermission'
  import {BaseForm, RxDialog, Dialog,Util} from "@lowcode/jpaas-common-lib";
  import ARow from "ant-design-vue/es/grid/Row";
  import ACol from "ant-design-vue/es/grid/Col";
  import FormUserOrGroup from "./FormUserOrGroup";


  export default {
    name: 'FormUserPermissionEdit',
    mixins: [BaseForm],
    components: {
      ACol,
      Util,
      ARow,
      RxDialog,
      Dialog,
      FormUserOrGroup
    },
    props: {
      value: {
        type: String,
        default: ""
      },
        type: '',
        dataType: '',
        show: {
            type: Boolean,
            default: false
        },
    },
    data() {
      return {
        showList: false,
        typeList: [],
        allType: false,
        noneType: false,
        attachmentType: false,
        attachmentList: false,
        deleteAttach: false,
        downloadAttach: false,
        resultData:{}
      }
    },
    created() {
        //附件类型
        if(this.dataType == 'rx-upload'){
            this.attachmentType = true;
            this.attachmentList = true;
        }
        this.getPermissionList();
    },
    methods: {
      i18n(name,text,key){
          return this.$ti18(name,text,"formPermiss",key);
      },
      onChangeAll(e) {
        if (this.allType) {
          //选择所有人，则清空所有选择
          this.noneType = false;
          this.showList = false;
          if(this.attachmentType){
            this.attachmentList = true;
          }
        }
        this.changeType();
      },
      onChangeNone(e) {
        if (this.noneType) {
          //选择无权限，则清空所有选择
          this.allType = false;
          this.showList = false;
          this.attachmentList = false;
        }else{
            if(this.attachmentType){
                this.attachmentList = true;
            }
        }
        this.changeType();
      },
      onChangeShowList(e){
          if(this.showList){
              this.noneType = false;
              this.allType = false;
              if(this.attachmentType){
                  this.attachmentList = true;
              }
          }
          this.changeType();
      },
      changeType() {
        for (var i = 0; i < this.typeList.length; i++) {
          var obj = this.typeList[i];
          obj.values = "";
          obj.names = "";
        }
      },
      getPermissionList() {
        var serf_=this;
        FormPermissionApi.getPermissionList().then(res => {
          serf_.setTypeValue(res.data);
        });
      },
      setTypeValue(data) {
          if(!this.show && (!this.value || this.value=='none' || this.value=='everyone')){
              this.value = "custom";
          }
        this.typeList = data;
        if(this.value.indexOf("everyone") > -1 && (this.value.indexOf("noneAttach") > -1 || this.value.indexOf("deleteAttach") > -1 || this.value.indexOf("downloadAttach") > -1)){
            var arr = this.value.split(",");
            for(var i = 0; i < arr.length; i++){
                this.value = arr[0];
                if(arr[i] == 'deleteAttach'){
                    this.deleteAttach = true;
                }else if(arr[i] == 'downloadAttach'){
                    this.downloadAttach = true;
                }

            }
        }
        if (!this.value || "everyone" == this.value ) {
            this.allType = true;
            //默认勾选
            if(this.type=='edit'){
                this.deleteAttach = true;
            }
            this.downloadAttach = true;
        } else if ("none" == this.value) {
          this.noneType = true;
          this.attachmentList = false;
        } else if ("custom" == this.value){
          this.showList = true;
        }else {
          this.showList = true;
          var selectObj=JSON.parse(this.value);
          //附件类型
          for (var key in selectObj){
                if(key == 'attachment'){
                    var val = selectObj[key].values;
                    var arr = val.split(",");
                    if(arr.length > 0){
                        for(var j = 0; j < arr.length; j++){
                            if(arr[j] == 'deleteAttach'){
                                this.deleteAttach = true;
                            }else if(arr[j] == 'downloadAttach'){
                                this.downloadAttach = true;
                            }

                        }
                    }

                    break;
                }
          }

          for(var i=0;i<this.typeList.length;i++){
            var typeObj =this.typeList[i];
            var isContinue=false;
            for (var key in selectObj){
              if(key ==typeObj.type){
                typeObj.values=selectObj[key].values;
                typeObj.names=selectObj[key].names;
                typeObj.include=selectObj[key].include;
                isContinue=true;
                break;
              }
            }
            if(isContinue){
              continue;
            }
          }
          this.showList = true;
        }
      },
      okBtn() {
        Util.closeWindow(this,"ok",this.getValues());
      },

      cancel() {
        Util.closeWindow(this,"cancel");
      },
      getValues() {
        if (this.noneType) {
          var resultData = {
            label: this.i18n('list.none','无权限'),
            value: "none"
          }
          return resultData;
        } else if (this.allType) {
            var resultData = {};
            if(this.attachmentType && this.deleteAttach  && this.downloadAttach){
                resultData = {
                    label: this.i18n('permission1','所有人,附件权限：删除、下载'),
                    value: "everyone,deleteAttach,downloadAttach"
                }
            }else if(this.attachmentType && this.deleteAttach  && !this.downloadAttach){
                resultData = {
                    label: this.i18n('permission2','所有人,附件权限：删除'),
                    value: "everyone,deleteAttach"
                }
            }else if(this.attachmentType && !this.deleteAttach  && this.downloadAttach){
                resultData = {
                    label: this.i18n('permission3','所有人,附件权限：下载'),
                    value: "everyone,downloadAttach"
                }
            }else if(this.attachmentType && !this.deleteAttach  && !this.downloadAttach){
                resultData = {
                    label: this.i18n('permission4','所有人,附件权限：无'),
                    value: "everyone,noneAttach"
                }
            }else{
                resultData = {
                    label: this.i18n('list.everyone','所有人'),
                    value: "everyone"
                }
            }

          return resultData;
        } else {
          var names = [];
          var typeListObj ={};
          for(var i=0;i<this.typeList.length;i++){
            var typeObj = this.typeList[i];
            if(!typeObj.names){
              continue;
            }
            typeListObj[typeObj.type]={
              names:typeObj.names,
              values:typeObj.values,
              include:typeObj.include
            };
            names.push(typeObj.name+":"+typeObj.names);
          }

          if(names.length>0){
              var resultData = {};
              if(this.attachmentType && this.deleteAttach  && this.downloadAttach){
                  typeListObj['attachment']={
                      names:this.i18n('permission5','附件权限：删除、下载'),
                      values:'deleteAttach,downloadAttach'
                  };
                  resultData = {
                      label: names.join(",") + "," + this.i18n('permission5','附件权限：删除、下载'),
                      value: JSON.stringify(typeListObj)
                  }
              }else if(this.attachmentType && this.deleteAttach  && !this.downloadAttach){
                  typeListObj['attachment']={
                      names:this.i18n('permission6','附件权限：删除'),
                      values:'deleteAttach'
                  };
                  resultData = {
                      label: names.join(",") + "," + this.i18n('permission6','附件权限：删除'),
                      value: JSON.stringify(typeListObj)
                  }
              }else if(this.attachmentType && !this.deleteAttach  && this.downloadAttach){
                  typeListObj['attachment']={
                      names:this.i18n('permission7','附件权限：下载'),
                      values:'downloadAttach'
                  };
                  resultData = {
                      label: names.join(",") + "," + this.i18n('permission7','附件权限：下载'),
                      value: JSON.stringify(typeListObj)
                  }
              }else if(this.attachmentType && !this.deleteAttach  && !this.downloadAttach){
                  typeListObj['attachment']={
                      names:this.i18n('permission8','附件权限：无'),
                      values:'noneAttach'
                  };
                  resultData = {
                      label: names.join(",") + "," + this.i18n('permission8','附件权限：无'),
                      value: JSON.stringify(typeListObj)
                  }
              }else{
                  resultData = {
                      label: names.join(","),
                      value: JSON.stringify(typeListObj)
                  };
              }


          }else{
            var resultData = {
              label: this.i18n('list.noConfig','无配置'),
              value: "custom"
            };
          }
          return resultData;
        }
      },
    }
  }
</script>

<style scoped>
  .myHeader{
    padding: 20px 20px 10px;
  }
  .container-box{
    padding: 8px 8px;
  }
  .container-box>li{
    margin-bottom: 5px;
    padding: 5px 5px 5px 5px !important;
  }
</style>